{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "SimSwap_images.ipynb",
      "private_outputs": true,
      "provenance": [],
      "collapsed_sections": [
        "xTo1C8aPUoOh",
        "rcoRX8OnVImS"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7_gtFoV8BuRx"
      },
      "source": [
        "# SimSwap\n",
        "Reference: https://github.com/neuralchen/SimSwap"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "D4fUfjQYUlYz"
      },
      "source": [
        "## Prepare code"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VA_4CeWZCHLP"
      },
      "source": [
        "%cd /content\n",
        "!git clone https://github.com/woctezuma/SimSwap.git\n",
        "%cd /content/SimSwap/\n",
        "!git checkout no-logo"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y5K4au_UCkKn"
      },
      "source": [
        "!pip install insightface==0.2.1 onnxruntime moviepy > /dev/null\n",
        "!pip install googledrivedownloader > /dev/null\n",
        "!pip install imageio==2.4.1 > /dev/null"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xTo1C8aPUoOh"
      },
      "source": [
        "## Prepare models"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BQGu8shAWOPE"
      },
      "source": [
        "%cd /content/SimSwap"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gLti1J0pEFjJ"
      },
      "source": [
        "from google_drive_downloader import GoogleDriveDownloader\n",
        "\n",
        "GoogleDriveDownloader.download_file_from_google_drive(file_id='1TLNdIufzwesDbyr_nVTR7Zrx9oRHLM_N',\n",
        "                                    dest_path='./arcface_model/arcface_checkpoint.tar')\n",
        "\n",
        "GoogleDriveDownloader.download_file_from_google_drive(file_id='1PXkRiBUYbu1xWpQyDEJvGKeqqUFthJcI',\n",
        "                                    dest_path='./checkpoints.zip')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2iwPorp0Mnvw"
      },
      "source": [
        "!wget --no-check-certificate \\\n",
        " https://sh23tw.dm.files.1drv.com/y4mmGiIkNVigkSwOKDcV3nwMJulRGhbtHdkheehR5TArc52UjudUYNXAEvKCii2O5LAmzGCGK6IfleocxuDeoKxDZkNzDRSt4ZUlEt8GlSOpCXAFEkBwaZimtWGDRbpIGpb_pz9Nq5jATBQpezBS6G_UtspWTkgrXHHxhviV2nWy8APPx134zOZrUIbkSF6xnsqzs3uZ_SEX_m9Rey0ykpx9w \\\n",
        " -O antelope.zip"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lKAcJ4ngVEUV"
      },
      "source": [
        "!unzip ./checkpoints.zip  -d ./checkpoints\n",
        "\n",
        "!unzip antelope.zip -d ./insightface_func/models/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_ZD8GPHsU4lN"
      },
      "source": [
        "## Prepare data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "T3YlcuLdVyZg"
      },
      "source": [
        "### Download"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f8SAi2SYSFmN"
      },
      "source": [
        "%cd /content\n",
        "\n",
        "!wget https://i.imgur.com/QYJOzy7.jpeg -O cpc_ackboo.jpg\n",
        "!wget https://i.imgur.com/l5MGOws.jpeg -O starwars_meme.jpg"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g-y81Z5dV3pR"
      },
      "source": [
        "input_fname = '/content/cpc_ackboo.jpg'\n",
        "output_fname = '/content/starwars_meme.jpg'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rcoRX8OnVImS"
      },
      "source": [
        "### Convert to JPG\n",
        "\n",
        "Images should not be too large, hence the (arbitrary) limitation of 1024 length."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CLf-l1tjTY0W"
      },
      "source": [
        "def get_new_size(img_size,\n",
        "                 max_allowed_length = 1024):\n",
        "\n",
        "  if any(max_allowed_length < sz for sz in img_size):\n",
        "    long_length = max(img_size)\n",
        "    ratio = max_allowed_length / long_length\n",
        "  else:\n",
        "    ratio = 1.0\n",
        "\n",
        "    new_img_size = [\n",
        "                    int(ratio*sz)\n",
        "                    for sz in img_size\n",
        "                    ]\n",
        "\n",
        "  return tuple(new_img_size)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UEOmN-DdPB5g"
      },
      "source": [
        "from PIL import Image\n",
        "\n",
        "allow_resize = False\n",
        "\n",
        "for fname in [input_fname, output_fname]:\n",
        "  jpg_fname = fname.replace('.png', '.jpg')\n",
        "\n",
        "  try:\n",
        "    img = Image.open(fname)\n",
        "  except FileNotFoundError:\n",
        "    continue\n",
        "\n",
        "  new_size = get_new_size(img.size, max_allowed_length = 1024)\n",
        "  if allow_resize:\n",
        "    print(f'Resizing from {img.size} to {new_size}')\n",
        "    img.resize(new_size)\n",
        "\n",
        "  print(f'Saving to {jpg_fname}')\n",
        "  img.convert('RGB').save(jpg_fname)\n",
        "\n",
        "jpg_input = input_fname.replace('.png', '.jpg')\n",
        "jpg_output = output_fname.replace('.png', '.jpg')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VZBoCtU9U8B3"
      },
      "source": [
        "## Run"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tZ_ZBtDQVLco"
      },
      "source": [
        "### Single"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zywghoUNOAmS"
      },
      "source": [
        "%cd /content/SimSwap\n",
        "%mkdir -p /content/output/single/\n",
        "\n",
        "!python test_wholeimage_swapsingle.py \\\n",
        " --isTrain false  --name people \\\n",
        " --Arc_path arcface_model/arcface_checkpoint.tar \\\n",
        " --pic_a_path {jpg_input} \\\n",
        " --pic_b_path {jpg_output} \\\n",
        " --output_path /content/output/single/ > /dev/null\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9MxvjygsVNb4"
      },
      "source": [
        "### Multi"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PudoS9JBRsOs"
      },
      "source": [
        "%cd /content/SimSwap\n",
        "%mkdir -p /content/output/multi/\n",
        "\n",
        "!python test_wholeimage_swapmutil.py \\\n",
        " --isTrain false  --name people \\\n",
        " --Arc_path arcface_model/arcface_checkpoint.tar \\\n",
        " --pic_a_path {jpg_input} \\\n",
        " --pic_b_path {jpg_output} \\\n",
        " --output_path /content/output/multi/ > /dev/null\n"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}